package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;


import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("BrandService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
     @Autowired
     private BrandMapper brandMapper;
    @Override
    public Result findAll() {
        List<Brand> list = this.list();
        return new Result(true, StatusCode.OK,null,list);
    }

    @Override
    public Result add(Brand brand) {
        brand.setId(null);
        boolean save = this.save(brand);
        return new Result(true,StatusCode.OK,null,save);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        Page<Brand> brandPage = new Page<>(page,size);
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery(Brand.class);
        //this.page(brandPage,wrapper);
        brandMapper.selectPage(brandPage,wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(brandPage.getTotal());
        pageResult.setRows(brandPage.getRecords());
        //PageResult<Brand> brandPageBean = new ToPageBeanUtils<Brand>().toPageBean(brandPage);
        //return new Result(true,StatusCode.OK,null,brandPageBean);
        return new Result(true,StatusCode.OK,null,pageResult);
    }

    @Override
    public Result findById(Integer id) {
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery(Brand.class).eq(Brand::getId,id);
        Brand brand = brandMapper.selectOne(wrapper);
        return new Result(true,StatusCode.OK,null,brand);
    }

    @Override
    public Result updateId(Integer id,Brand brand) {
        LambdaUpdateWrapper<Brand> wrapper = Wrappers.lambdaUpdate(Brand.class).eq(Brand::getId, id)
                .set(Brand::getImage,brand.getImage()).set(Brand::getLetter,brand.getLetter()).set(Brand::getName,brand.getName())
                .set(Brand::getSeq,brand.getSeq());
        int update = brandMapper.update(null, wrapper);
        if (update>0) {
            return new Result(true,StatusCode.OK,null,true);
        }
        return new Result(true,StatusCode.ERROR,null,false);
    }

    @Override
    public Result deleteId(Integer id) {
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery(Brand.class).eq(Brand::getId, id);
        int delete = brandMapper.delete(wrapper);
        if (delete>0) {
            return new Result(true,StatusCode.OK,null,true);
        }
        return new Result(true,StatusCode.ERROR,null,false);
    }

}
